#! /bin/sh

# Write EEPROM to Amp100 and play test tone

OUT=/dev/tty1

# Cleanup some stuff
rm /etc/init.d/S49ntp 2>/dev/null
rm /etc/init.d/S90sigmatcp 2>/dev/null

case "$1" in
    start)
	i2c=`cat /boot/config.txt | grep i2c-gpio`
        if [ "$i2c" == "" ]; then
           echo "dtoverlay=i2c-gpio" >> /boot/config.txt
	   echo "dtparam=i2c_gpio_sda=0" >> /boot/config.txt
	   echo "dtparam=i2c_gpio_scl=1" >> /boot/config.txt
        fi

	# Program EEPROM
        PATH=$PATH:/opt/hifiberry/contrib
        export PATH
        /opt/hifiberry/contrib/hbflash.sh -w -f=/opt/hifiberry/contrib/amp100.eep -t=24c32

	# Reset Amp100
	echo 17 >/sys/class/gpio/export
	echo 4  >/sys/class/gpio/export
	echo out >/sys/class/gpio/gpio17/direction
	echo out >/sys/class/gpio/gpio4/direction
	# Mute
	echo 1 >/sys/class/gpio/gpio4/value
	# Reset
	echo 0 >/sys/class/gpio/gpio17/value
	echo 1 >/sys/class/gpio/gpio17/value
	# Unmute
        echo 0 >/sys/class/gpio/gpio4/value

	amixer sset Digital 70%
        play -n synth 60 sine 1000  &
	;;
    stop)
	;;
    restart)
	$0 stop
	$0 start
	;;
    *)
	echo "Usage: $0 {start|stop|restart}"
	exit 1
	;;
esac

